package com.tebiecloud.base.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.base.client.model.entity.BaseUser;
import com.tebiecloud.base.server.service.BaseUserService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 系统用户-管理员信息 前端控制器
 * @author tebie
 * @date 2019-08-08
 */
@RestController
@RequestMapping("baseuser")
public class BaseUserController {

    @Autowired
    private BaseUserService baseUserService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "/list")
    public ResultBody<IPage<BaseUser>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        BaseUser query = pageParams.mapToObject(BaseUser.class);
        QueryWrapper<BaseUser> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(baseUserService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("/get")
    public ResultBody<BaseUser> get(@RequestParam("id") Long id) {
        BaseUser entity = baseUserService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param userName 登陆账号,
     * @param nickName 昵称,
     * @param avatar 头像,
     * @param email 邮箱,
     * @param mobile 手机号,
     * @param userType 用户类型:super-超级管理员 normal-普通管理员,
     * @param companyId 企业ID,
     * @param userDesc 描述,
     * @param status 状态:0-禁用 1-正常 2-锁定
     * @return
     */
    @PostMapping("/add")
    public ResultBody add(
        @RequestParam(value = "userName", required = false) String userName,
        @RequestParam(value = "nickName", required = false) String nickName,
        @RequestParam(value = "avatar", required = false) String avatar,
        @RequestParam(value = "email", required = false) String email,
        @RequestParam(value = "mobile", required = false) String mobile,
        @RequestParam(value = "userType", required = false) String userType,
        @RequestParam(value = "companyId", required = false) Long companyId,
        @RequestParam(value = "userDesc", required = false) String userDesc,
        @RequestParam(value = "status", required = false) Integer status
    ) {
        BaseUser entity = new BaseUser();
        entity.setUserName(userName);
        entity.setNickName(nickName);
        entity.setAvatar(avatar);
        entity.setEmail(email);
        entity.setMobile(mobile);
        entity.setUserType(userType);
        entity.setCompanyId(companyId);
        entity.setUserDesc(userDesc);
        entity.setStatus(status);
        baseUserService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param userName 登陆账号
     * @param nickName 昵称
     * @param avatar 头像
     * @param email 邮箱
     * @param mobile 手机号
     * @param userType 用户类型:super-超级管理员 normal-普通管理员
     * @param companyId 企业ID
     * @param userDesc 描述
     * @param status 状态:0-禁用 1-正常 2-锁定
     * @return
     */
    @PostMapping("/update")
    public ResultBody update(
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "nickName", required = false) String nickName,
            @RequestParam(value = "avatar", required = false) String avatar,
            @RequestParam(value = "email", required = false) String email,
            @RequestParam(value = "mobile", required = false) String mobile,
            @RequestParam(value = "userType", required = false) String userType,
            @RequestParam(value = "companyId", required = false) Long companyId,
            @RequestParam(value = "userDesc", required = false) String userDesc,
            @RequestParam(value = "status", required = false) Integer status
    ) {
        BaseUser entity = new BaseUser();
        entity.setUserId(userId);
        entity.setUserName(userName);
        entity.setNickName(nickName);
        entity.setAvatar(avatar);
        entity.setEmail(email);
        entity.setMobile(mobile);
        entity.setUserType(userType);
        entity.setCompanyId(companyId);
        entity.setUserDesc(userDesc);
        entity.setStatus(status);
        baseUserService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("/remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            baseUserService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            baseUserService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
